package com.cyan.app.workflow.fragment;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cyan.app.workflow.AppData;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.activity.UserCenter.UserCenterActivity;
import com.cyan.app.workflow.base.fragment.BaseFragment;
import com.cyan.app.workflow.common.LoginProxy;

/**
 * Created by GivenTang on 2015/11/1.
 */
public class UserCenterFragment extends BaseFragment implements View.OnClickListener {
    public static final String TAG = "UserCenterFragment";

    private TextView top_bar_middle;
    private RelativeLayout out_login;
    private TextView mTextViewName;
    private View update;
    private View user;
    private Button mButton;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        _RootView = inflater.inflate(R.layout.layout_settings, null);
        return _RootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();

        //用户中心
        user = _RootView.findViewById(R.id.user_layout);
        user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), UserCenterActivity.class);
                getActivity().startActivity(intent);
            }
        });
        //更新
        update = _RootView.findViewById(R.id.check_new_version);
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mTextViewName = (TextView)_RootView.findViewById(R.id.user_nick_name);
        mTextViewName.setText(AppData.getInstance().getUserName());
    }

    private void initView(){
        top_bar_middle = (TextView) _RootView.findViewById(R.id.top_bar_middle);
        top_bar_middle.setText(R.string.setting);

        out_login = (RelativeLayout) _RootView.findViewById(R.id.out_login);
        out_login.setOnClickListener(this);
        mTextViewName = (TextView)_RootView.findViewById(R.id.user_nick_name);
        mTextViewName.setText(AppData.getInstance().getUserName());
        mButton = (Button)_RootView.findViewById(R.id.set_info_exit);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.out_login:
                showPopupWindow();
                break;
            case R.id.set_info_exit:
                showPopupWindow();
                break;
        }
    }

    private void showPopupWindow() {
        final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.AnimBottom);
        window.setContentView(R.layout.login_out_pop);
        window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        alertDialog.setCancelable(true);
        alertDialog.setCanceledOnTouchOutside(true);

        ((TextView) window.findViewById(R.id.confirm_quit)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                LoginProxy.getInstance(getActivity()).logout();
            }
        });
        ((TextView) window.findViewById(R.id.confirm_cancel)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
    }

}
